// stdafx.cpp : source file that includes just the standard includes
//	SinaBlog.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"



// Get the version of a file
//
CString GetFileVersion()
{
	char cPath[MAX_PATH];
	::GetModuleFileName(NULL,cPath,sizeof(cPath));
	
	CString version;
	VS_FIXEDFILEINFO *pVerInfo = NULL;
	DWORD	dwTemp, dwSize, dwHandle = 0;
	BYTE	*pData = NULL;
	UINT	uLen;
	
	try {
		dwSize = GetFileVersionInfoSize((LPTSTR) cPath, &dwTemp);
		if (dwSize == 0) throw 1;
		
		pData = new BYTE[dwSize];
		if (pData == NULL) throw 1;
		
		if (!GetFileVersionInfo((LPTSTR) cPath, dwHandle, dwSize, pData))
			throw 1;
		
		if (!VerQueryValue(pData, _T("\\"), (void **) &pVerInfo, &uLen)) 
			throw 1;
		
		DWORD verMS = pVerInfo->dwFileVersionMS;
		DWORD verLS = pVerInfo->dwFileVersionLS;
		
		// Are lo-words used?
		if (HIWORD(verLS) != 0 || LOWORD(verLS) != 0)
		{
			version.Format(_T("%d.%d.%d.%d"), HIWORD(verMS), LOWORD(verMS), HIWORD(verLS), LOWORD(verLS));
		}
		else if (HIWORD(verMS) != 0 || LOWORD(verMS) != 0)
		{
			version.Format(_T("%d.%d"), HIWORD(verMS), LOWORD(verMS));
		}
		
		delete pData;
		return version;
	}
	catch(...) {
		return _T("");
	}	
}


/********************************************************************/
/*																	*/
/* Function name : MakeSureDirectoryPathExists						*/
/* Description   : This function creates all the directories in		*/
/*				   the specified DirPath, beginning with the root.	*/
/*				   This is a clone a Microsoft function with the	*/
/*			       same name.										*/
/*																	*/
/********************************************************************/
BOOL CreateDirectoryPath(LPCTSTR lpszDirPath)
{
	CString strDirPath = lpszDirPath;
	
	int nPos = 0;
	
	while((nPos = strDirPath.Find('/', nPos+1)) != -1) 
	{
		CreateDirectory(strDirPath.Left(nPos), NULL);
	}
	return CreateDirectory(strDirPath, NULL);
}

// Test Whether the dir exist
BOOL MakeSureDirExist(CString strDirPath)
{
	strDirPath.Replace("\\", "/");
	
	if (strDirPath.Right(1) == "/")
		strDirPath = strDirPath.Left(strDirPath.GetLength()-1);
	
	if (GetFileAttributes(strDirPath) == FILE_ATTRIBUTE_DIRECTORY)
		return TRUE;
	else
		CreateDirectoryPath(strDirPath); 
	
	return FALSE;
}
